﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;
using AutoMapper;

namespace KernelWorkflow.Activities
{
    public partial class GetStudentsWithGroupsActivity : SequenceActivity
    {
        public static readonly string AuthGetStudentsWithGroupsActivityAccessObject = "GetStudentsWithGroups";
        public bool crutch_bootstrap = true;
        public Boolean IsSessionOk = false;

        public GroupListDto GLISTDTO = null;
        public IEnumerator<GroupBaseDto> GroupBaseEnumerator;
        public IEnumerator<Guid> StudentGuidEnumerator;

        public Guid CurrentGroupGuid = Guid.Empty;
        public StudentListGuidsDto guidsofstudents = null;

        public GroupWithStudentsDto GWS = null;
        public List<StudentDto> GWS_STDTO = null;

        public StudentDto CurrentStudent = null;
        public Guid CurrentStudentGuid = Guid.Empty;

        public List<GroupWithStudentsDto> ResultListOfGroups = null;



        public static DependencyProperty CurrentSessionProperty = DependencyProperty.Register("CurrentSession", typeof(SessionDto), typeof(GetStudentsWithGroupsActivity));
        public static DependencyProperty StudentsWithGroupsProperty = DependencyProperty.Register("StudentsWithGroups", typeof(StudentsWithGroupsDto), typeof(GetStudentsWithGroupsActivity));



        [Category("AUTHORIZATION_INPUT")]
        [Browsable(true)]
        public SessionDto CurrentSession
        {
            get
            {
                return ((SessionDto)(base.GetValue(GetStudentsWithGroupsActivity.CurrentSessionProperty)));
            }
            set
            {
                base.SetValue(GetStudentsWithGroupsActivity.CurrentSessionProperty, value);
            }
        }

        [Category("DATA_OUTPUT")]
        [Browsable(true)]
        public StudentsWithGroupsDto StudentsWithGroups
        {
            get
            {
                return ((StudentsWithGroupsDto)(base.GetValue(GetStudentsWithGroupsActivity.StudentsWithGroupsProperty)));
            }
            set
            {
                base.SetValue(GetStudentsWithGroupsActivity.StudentsWithGroupsProperty, value);
            }
        }


        public GetStudentsWithGroupsActivity()
        {
            InitializeComponent();
        }        


        private void CheckRightsAfterResponse(object sender, SendActivityEventArgs e)
        {
            Console.WriteLine("CheckRightsAfterResponse this.isSessionOk={0}", this.IsSessionOk);
        }

        private void GetResultListOfGroups(object sender, SendActivityEventArgs e)
        {
            ResultListOfGroups = new List<GroupWithStudentsDto>();
            StudentsWithGroups = new StudentsWithGroupsDto();
            StudentsWithGroups.Groups = ResultListOfGroups;

            if (crutch_bootstrap)
            {
                Mapper.CreateMap<GroupBaseDto, GroupWithStudentsDto>();
                crutch_bootstrap = false;
            }

            Console.WriteLine("GetResultListGroups begin");
        }
        private void GetGroupBaseEnumerator(object sender, SendActivityEventArgs e)
        {
            GroupBaseEnumerator = GLISTDTO.Groups.GetEnumerator();
            Console.WriteLine("GetGroupBaseEnumerator");
        }
        private void MakeGroupWithStudents(object sender, SendActivityEventArgs e)
        {
            GWS = Mapper.Map<GroupBaseDto, GroupWithStudentsDto>(GroupBaseEnumerator.Current);
            GWS_STDTO = new List<StudentDto>();
            GWS.Students = GWS_STDTO;

            CurrentGroupGuid = GroupBaseEnumerator.Current.Id;
            Console.WriteLine("MakeGroupWithStudents");
        }
        private void GetStudentDtoEnumerator(object sender, SendActivityEventArgs e)
        {
            StudentGuidEnumerator = guidsofstudents.Guids.GetEnumerator();
            Console.WriteLine("GetStudentDtoEnumerator");
        }
        private void SetCurrentStudentGuid(object sender, SendActivityEventArgs e)
        {
            CurrentStudentGuid = StudentGuidEnumerator.Current;
            Console.WriteLine("SetCurrentStudentGuid");
        }
        private void AddCurrentStudentToCurrentGroup(object sender, SendActivityEventArgs e)
        {
            GWS_STDTO.Add(CurrentStudent);
            Console.WriteLine("AddCurrentStudentToCurrentGroup");
        }

        private void AddCurrentGroup(object sender, EventArgs e)
        {
            ResultListOfGroups.Add(GWS);
            Console.WriteLine("AddCurrentGroup");
        }

        private void __debugCodeActivity_Execute(object sender, EventArgs e)
        {
            Console.WriteLine("StudentsWithGroups finish");

            foreach (var mygroup in StudentsWithGroups.Groups)
            {
                Console.WriteLine(" GroupId={0}, GroupName={1}", mygroup.Id, mygroup.Name);
                foreach (var mystudent in mygroup.Students)
                    Console.WriteLine("  StudentId={0}, StudentName={1}", mystudent.Id, mystudent.Name);
            }
                        
        }
    }
}
